﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Data;

public partial class KUpi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        proverka_kosnicka();
        lblKorisnik.Text ="Најавен корисник: "+ (string)Session["korisnik"];
        if (isUserLoggedIn())
        {
            btnOdlogiranje.Visible = true;
            lblKorisnickoIMe.Text = (string)Session["korisnik"];
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblNaslov.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }
            return;
        }
        else
        {
            btnLogiranje.Visible = true;

            Response.Redirect("Login.aspx");
        }
        
        if (!IsPostBack)
        {
            
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblNaslov.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }
        }
    }
    //protected void ispolni_grid_kupi()
    //{
    //    SqlConnection konekcija = new SqlConnection();
    //    konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
    //    string sqlString = "SELECT * FROM Kupi";
    //    SqlCommand komanda = new SqlCommand(sqlString, konekcija);
    //    SqlDataAdapter adapter = new SqlDataAdapter(komanda);
    //    DataSet ds = new DataSet();
    //    try
    //    {
    //        konekcija.Open();
    //        adapter.Fill(ds, "Kupi");
    //        gvKupi.DataSource = ds;
    //        gvKupi.DataBind();
    //        ViewState["dataset"] = ds;
    //    }
    //    catch (Exception err)
    //    {
            
    //    }
    //    finally
    //    {
    //        konekcija.Close();
    //    }

    //}
    protected void proverka_kosnicka()
    {
        ArrayList lista;
        if (Session["kosnicka"] != null)
        {
            int vkupno=0;
            lista = (ArrayList)Session["kosnicka"];
            lstKupi.DataSource = lista;
            lstKupi.DataBind();
            foreach (ListItem l in lista)
                vkupno += Convert.ToInt32(l.Value);
            lblCena.Text =  vkupno.ToString();
        }
    }
    private bool isUserLoggedIn()
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return true;
            }
        }
        return false;
    }
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (Menu1.SelectedValue == "Маици")
            Response.Redirect("za_mazi.aspx?tip=maici");
        if (Menu1.SelectedValue == "Патики")
            Response.Redirect("za_mazi.aspx?tip=patiki");
        if (Menu1.SelectedValue == "Панталони")
            Response.Redirect("za_mazi.aspx?tip=pantaloni");
        if (Menu1.SelectedValue == "Фустани")
            Response.Redirect("za_zeni.aspx?tip=fustani");
        if (Menu1.SelectedValue == "Штикли")
            Response.Redirect("za_zeni.aspx?tip=stikli");
        if (Menu1.SelectedValue == "Капи")
            Response.Redirect("za_zeni.aspx?tip=kapi");
        if (Menu1.SelectedValue == "Облека за мажи")
            Response.Redirect("za_mazi.aspx?tip=mazi");
        if (Menu1.SelectedValue == "Облека за жени")
            Response.Redirect("za_zeni.aspx?tip=zeni");
    }
    protected void btnLogiranje_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
    protected void btnOdlogiranje_Click(object sender, EventArgs e)
    {

        FormsAuthentication.SignOut();

        // kolaceto istekuva vcera, t.e. ne e validno
        if (FormsAuthentication.FormsCookieName != null)
        {
            Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(-1);
        }

        // prefrli na default stranata
        Response.Redirect("Doma.aspx");
    }
  
    protected void btnPlati_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < gvKlienti.Rows.Count; i++)
        {
            if (gvKlienti.Rows[i].Cells[3].Text == txtKarticka.Text)
            {
                dodadi_kupi();
                izbrisi_pom();
                Session["kosnicka"] = null;
               
                Response.Redirect("Doma.aspx");
                
            }
            else
            {
                lblGreska.Text = "Внесете точен број на кредитна картичка";
            }
        }
        
        
    }
    protected void dodadi_kupi()
    {
        
        for(int i=0; i<gvPom.Rows.Count;i++)
        {
            int cena = 0, sifra;
            string ime, korisnik;
            DateTime datum;
            datum = DateTime.Now;
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            cena=Convert.ToInt32(gvPom.Rows[i].Cells[2].Text);
            sifra=Convert.ToInt32(gvPom.Rows[i].Cells[0].Text);
            ime=gvPom.Rows[i].Cells[1].Text;
            korisnik=lblKorisnickoIMe.Text;
            string sqlString = "INSERT INTO Kupi values('" + sifra + "','" + korisnik + "','" + ime + "','" + cena + "','" + datum + "')";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }
            catch (Exception err)
            {
            }
            finally
            {
                konekcija.Close();
            }
        }
    }
    protected void btnOtkazi_Click(object sender, EventArgs e)
    {
        izbrisi_pom();
        lstKupi.Items.Clear();
        lblCena.Text = "";
        Session["kosnicka"] = null;
        Response.Redirect("Doma.aspx");
    }
    protected void izbrisi_pom()
    {
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        string sqlString = "DELETE FROM pom";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        try
        {
            konekcija.Open();
            komanda.ExecuteNonQuery();
        }
        catch (Exception err)
        {
        }
        finally
        {
            konekcija.Close();
        }
    }
}